﻿using System;
using System.Collections.Generic;
using System.Web;
using CrazyTalk.BLL;
using CrazyTalk.Common;
using CrazyTalk.Config;
using CrazyTalk.Models;

namespace CrazyTalk.Web.UI
{
    public class Facade
    {
        const string RemebUid = "tg_remebUid";
        const string CrazyTalkTg = "CrazyTalk_tg";
        const string Ug = "_ug";
        const string Sign = "Sign";
        /// <summary>
        /// 方法： void SetRemebUid
        /// 设置记住当前用户ID
        /// </summary> 
        public static void SetRemebUid(string userid)
        {
            CookieHelper.SetCookie(RemebUid, ConvertUtil.ToBase64(userid), DateTime.Now.AddDays(ConfigHelper.GetConfigInt(Consts.SetRemebTime)));
        }
        /// <summary> 
        /// 方法： string GetRemebUid
        /// 获取记住当前用户ID
        /// </summary> 
        public static string GetRemebUid()
        {
            return ConvertUtil.FromBase64(CookieHelper.GetCookieValue(RemebUid));
        }

        /// <summary>
        /// 方法： void RemoveRemebUid
        /// 移除记住用户Cookie
        /// </summary> 
        public static void RemoveRemebUid()
        {
            CookieHelper.ClearCookie(RemebUid);
        }

        const string Remeb = "tg_remeb";
        /// <summary>
        /// 方法： void SetRemebUid
        /// 设置记住当前状态
        /// </summary> 
        public static void SetRemeb(bool isChecked)
        {
            CookieHelper.SetCookie(Remeb, ConvertUtil.ToBase64(isChecked.ToString()), DateTime.Now.AddDays(ConfigHelper.GetConfigInt(Consts.SetRemebTime)));
        }

        /// <summary> 
        /// 方法： string GetRemebUid
        /// 获取记住当前状态
        /// </summary> 
        public static bool GetRemeb()
        {
            string flg = ConvertUtil.FromBase64(CookieHelper.GetCookieValue(Remeb));
            if (!string.IsNullOrEmpty(flg))
            {
                return bool.Parse(flg);
            }
            return false;
        }

        
        public static void RemoveRemeb()
        {
            CookieHelper.ClearCookie(Remeb);
        }
        public static string GetUserID()
        {
            //return ConvertUtil.FromBase64(CookieHelper.GetCookieValue(_uid));
            string userid= CookieHelper.GetCookieValue(CrazyTalkTg, Ug);
            string sign = CookieHelper.GetCookieValue(CrazyTalkTg, Sign);
            if (sign != Entry(userid))
            {
                return "";
            }
            return userid;            
        }
       
        public static void RemoveUserID()
        {
            CookieHelper.ClearCookie("CrazyTalk_tg");
        }

        public static void LogOut()
        {
            RemoveUserID();

        }

        /// <summary>
        /// 插入登录记录
        /// </summary>
        /// <param name="obj">obj=userinfo实体</param>
        public static void TraceLogin(object obj)
        {
            try
            {
                List<object> list = obj as List<object>;
                if (list != null)
                {
                    UserInfo userInfo = list[0] as UserInfo;
                    string ip = list[1] as string;
                    UserSession session = new UserSession();
                    session.UserToken = SequenceNo.CreateByGen('T').ToString();
                    if (userInfo != null)
                    {
                        session.UserID = userInfo.UserId;
                        session.RowID = userInfo.RoleId;
                        session.NickName = userInfo.NickName;
                    }
                    session.IP = ip;
                    session.LastActiveTime = DateTime.Now;
                    session.StampTime = DateTime.Now;
                    session.ActionType = "S";
                    session.Resource = "C";
                    UserSessionBLL.Instance.Add(session);
                }
            }
            catch (Exception e) { SakForNet.LoggerManager.GetLogger().Error("TraceLogin Error===>" + e); }
        }

        public static void SetLogin(string userid)
        {
            HttpContext context = HttpContext.Current;
            context.Response.AddHeader("P3P", "CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR");
            HttpCookie cookie = new HttpCookie(CrazyTalkTg);
            //model.UserId
            cookie.Values[Ug] = userid;
            cookie.Values[Sign] = Entry(userid);
            cookie.Expires = DateTime.Now.AddMinutes(Consts.LoginTimeOut);
            //cookie.Expires = DateTime.Now.AddMinutes(ConfigHelper.GetConfigInt(Consts.LoginExpiredTime));           
            //cookie.Expires = DateTime.Now.AddDays(1);
            context.Response.Cookies.Add(cookie);
        }

        private static string Entry(string userid)
        {
            return VistorUtils.BuildSign(string.Format("{0}tx{1}", userid, SecurityConfig.GetConfig().PasswordKey));
        }

    }
}
